/*
 * 描          述:  <描述>
 * 修  改   人:  PengQingyang
 * 修改时间:  2022年6月16日
 * <修改描述:>
 */
package com.tx.component.file.catalog;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

import com.tx.component.file.configurator.FCResourceLoaderBuilder;
import com.tx.component.file.configurator.VitualFileCatalogConfigurator;
import com.tx.core.exceptions.util.AssertUtils;

/**
 * 虚拟文件目录构建器<br/>
 * <功能详细描述>
 * 
 * @author  PengQingyang
 * @version  [版本号, 2022年6月16日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class VitualFileCatalogFactory
        implements FactoryBean<VitualFileCatalog>, InitializingBean {
    
    /** 虚拟文件目录配置器 */
    private VitualFileCatalogConfigurator configurator;
    
    /** 目录实例 */
    private DefaultVitualFileCatalog catalog;
    
    /** <默认构造函数> */
    public VitualFileCatalogFactory() {
        super();
    }
    
    /**
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        AssertUtils.notNull(configurator, "configurator is null.");
        AssertUtils.notEmpty(configurator.catalog(),
                "configurator.catalog is empty.");
        
        this.catalog = new DefaultVitualFileCatalog();
        FCResourceLoaderBuilder builder = this.configurator.builder();
        AssertUtils.notNull(builder, "configurator.builder is null.");
        ViewUrlHandler viewUrlHandler = this.configurator.viewUrlHandler();
        AssertUtils.notNull(viewUrlHandler,
                "configurator.viewUrlHandler is null.");
        
        this.catalog.setCatalog(this.configurator.catalog());
        //构建资源加载器
        this.catalog.setResourceLoader(
                builder.catalog(this.configurator.catalog()).build());
        this.catalog.setViewUrlHandler(viewUrlHandler);
        this.catalog.setPermission(this.configurator.permission());
    }
    
    /**
     * @return
     * @throws Exception
     */
    @Override
    public VitualFileCatalog getObject() throws Exception {
        return this.catalog;
    }
    
    /**
     * @return
     */
    @Override
    public Class<?> getObjectType() {
        return VitualFileCatalog.class;
    }
    
    /**
     * @return
     */
    @Override
    public boolean isSingleton() {
        return true;
    }
    
    /**
     * @return 返回 configurator
     */
    public VitualFileCatalogConfigurator getConfigurator() {
        return configurator;
    }
    
    /**
     * @param 对configurator进行赋值
     */
    public void setConfigurator(VitualFileCatalogConfigurator configurator) {
        this.configurator = configurator;
    }
}
